#include <iostream>
using namespace std;

class Base;

Base *pd;

class Printer
{
public:
    Printer(Base *p):pp(p) {}
    void Print();
private:
    Base *pp;
};

class Base
{
public:
    Base() {
        pp = new Printer(this);
    }
    virtual ~Base() {
        pp->Print();
        delete pp;
    }

    virtual void Do() = 0;
private:
    Printer *pp;
};

class Derived : public Base
{
public:
    Derived() {
        value = 22;
        cout << "ctr Derived" << endl;
    }
    virtual ~Derived() {
        value = 0;
        cout << "dsr Derived" << endl;
    }
    virtual void Do() {
        cout << "Derived: " << value << endl;
    }
private:
    int value;
};

void Printer::Print(){
    pd->Do();
}

int main()
{
    pd = new Derived();
    delete pd;
}
